package praca.framework;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

public class Vertices {

	ByteBuffer byteBuffer;
	FloatBuffer floatBuffer;
	ShortBuffer shortBuffer;
	int[] tmpBuffer;
	private boolean isBitmapVertex = false;
	private boolean isTriangleVertex = true;
	
	public FloatBuffer getFloatBuffer(){
		return floatBuffer;
	}
	
	public void setIndices(short[] indices){
		shortBuffer.clear();
		shortBuffer.put(indices);
		shortBuffer.flip();
	}
	
	public void setVertices(float[] vertices){
		floatBuffer.clear();
        floatBuffer.put(vertices);
        floatBuffer.flip();
		
		
	}
	
	public Vertices(int numberOfVertices, int numberOfIndices){
		isBitmapVertex = true;
		isTriangleVertex = false;
		
		byteBuffer = ByteBuffer.allocateDirect(16*numberOfVertices);
		byteBuffer.order(ByteOrder.nativeOrder());
		floatBuffer = byteBuffer.asFloatBuffer();

		byteBuffer = ByteBuffer.allocateDirect(2*numberOfIndices);
		byteBuffer.order(ByteOrder.nativeOrder());
		shortBuffer = byteBuffer.asShortBuffer();
		
	}
	
	public Vertices(int width, int height, float xSt, float xEnd, float ySt, float yEnd, boolean nieUzywac){
		isTriangleVertex = false;
		
		this.isBitmapVertex=true;
		
		byteBuffer = ByteBuffer.allocateDirect(64);
		byteBuffer.order(ByteOrder.nativeOrder());
		floatBuffer = byteBuffer.asFloatBuffer();
		floatBuffer.clear();
		floatBuffer.put(new float[]{
				-width/2, -height/2, xSt, ySt,
				-width/2, -height/2+height, xSt, yEnd,
				-width/2+width, -height/2+height, xEnd, yEnd,
				-width/2+width, -height/2, xEnd, ySt
				
		});
		floatBuffer.flip();
		
		byteBuffer = ByteBuffer.allocateDirect(12);
		byteBuffer.order(ByteOrder.nativeOrder());
		shortBuffer = byteBuffer.asShortBuffer();
		shortBuffer.clear();
		shortBuffer.put(new short[]{
				0,1,2,
				2,3,0
		});
		shortBuffer.flip();
		
	}
	
	public Vertices(float x, float y, float textureWidth, float textureHeight, 
			float width, float height, 
			float atlasWidth,
			float atlasHeight){
		isTriangleVertex = false;
		
		this.isBitmapVertex=true;
		
		byteBuffer = ByteBuffer.allocateDirect(64);
		byteBuffer.order(ByteOrder.nativeOrder());
		floatBuffer = byteBuffer.asFloatBuffer();
		floatBuffer.clear();
		floatBuffer.put(new float[]{
				-width/2, -height/2, x/atlasWidth, 1-y/atlasHeight,
				-width/2, -height/2+height, x/atlasWidth, 1-((y+textureHeight)/atlasHeight),
				-width/2+width, -height/2+height, (x+textureWidth)/atlasWidth, 1-((y+textureHeight)/atlasHeight),
				-width/2+width, -height/2, (x+textureWidth)/atlasWidth, 1-y/atlasHeight
				
				
		});
		//float iks = x/atlasWidth;
		//float igrek = 1-((y+textureHeight)/atlasHeight);
		
		//Log.d("vertices", "x: "+iks+" ,y: "+igrek);
		floatBuffer.flip();
		
		byteBuffer = ByteBuffer.allocateDirect(12);
		byteBuffer.order(ByteOrder.nativeOrder());
		shortBuffer = byteBuffer.asShortBuffer();
		shortBuffer.clear();
		shortBuffer.put(new short[]{
				0,1,2,
				2,3,0
		});
		shortBuffer.flip();
		
	}
	
	public void setVertices(float x, float y, float textureWidth, float textureHeight, 
			float width, float height, 
			float atlasWidth,
			float atlasHeight){
		isTriangleVertex = false;
		
		this.isBitmapVertex=true;
		

		floatBuffer.clear();
		floatBuffer.put(new float[]{
				-width/2, -height/2, x/atlasWidth, 1-y/atlasHeight,
				-width/2, -height/2+height, x/atlasWidth, 1-((y+textureHeight)/atlasHeight),
				-width/2+width, -height/2+height, (x+textureWidth)/atlasWidth, 1-((y+textureHeight)/atlasHeight),
				-width/2+width, -height/2, (x+textureWidth)/atlasWidth, 1-y/atlasHeight
				
				
		});
		//float iks = x/atlasWidth;
		//float igrek = 1-((y+textureHeight)/atlasHeight);
		
		//Log.d("vertices", "x: "+iks+" ,y: "+igrek);
		floatBuffer.flip();
		
		shortBuffer.clear();
		shortBuffer.put(new short[]{
				0,1,2,
				2,3,0
		});
		shortBuffer.flip();
		
	}
	
	public Vertices(float x1, float y1, float x2, float y2, boolean isBitmapVertex){
		isTriangleVertex = false;
		if(isBitmapVertex){
			this.isBitmapVertex=true;
			byteBuffer = ByteBuffer.allocateDirect(64);
			byteBuffer.order(ByteOrder.nativeOrder());
			floatBuffer = byteBuffer.asFloatBuffer();
			floatBuffer.clear();
			floatBuffer.put(new float[]{
					x1, y1, 0f, 1f,
					x1, y2, 0f, 0f,
					x2, y2, 1f, 0f,
					x2, y1, 1f, 1f
					
			});
			floatBuffer.flip();
			
			byteBuffer = ByteBuffer.allocateDirect(12);
			byteBuffer.order(ByteOrder.nativeOrder());
			shortBuffer = byteBuffer.asShortBuffer();
			shortBuffer.clear();
			shortBuffer.put(new short[]{
					0,1,2,
					2,3,0
			});
			shortBuffer.flip();
		}else{
			
		byteBuffer = ByteBuffer.allocateDirect(32);
		byteBuffer.order(ByteOrder.nativeOrder());
		floatBuffer = byteBuffer.asFloatBuffer();
		floatBuffer.clear();
		floatBuffer.put(new float[]{
				x1, y1, 
				x1, y2,
				x2, y2,
				x2, y1
				
		});
		floatBuffer.flip();
		
		byteBuffer = ByteBuffer.allocateDirect(12);
		byteBuffer.order(ByteOrder.nativeOrder());
		shortBuffer = byteBuffer.asShortBuffer();
		shortBuffer.clear();
		shortBuffer.put(new short[]{
				0,1,2,
				2,3,0
		});
		shortBuffer.flip();
		floatBuffer.position(0);
	}
	}
	
	public Vertices(float x1, float y1, float x2, float y2, float x3, float y3, boolean eee
			){
		isBitmapVertex = false;
		isTriangleVertex = true;
		
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * 2 * 4);
        byteBuffer.order(ByteOrder.nativeOrder());            
        floatBuffer = byteBuffer.asFloatBuffer();
        floatBuffer.put( new float[] {    x1,   y1,
                                     x2,   y2,
                                     x3, y3});
        floatBuffer.flip();

	}

	private Vertices(float x1, float y1, int width, int height, boolean isBitmapVertex){
		isTriangleVertex = false;
		
		if(isBitmapVertex){
			this.isBitmapVertex=true;
		
		byteBuffer = ByteBuffer.allocateDirect(64);
		byteBuffer.order(ByteOrder.nativeOrder());
		floatBuffer = byteBuffer.asFloatBuffer();
		floatBuffer.clear();
		floatBuffer.put(new float[]{
				x1, y1, 0f, 1f,
				x1, y1+height, 0f, 0f,
				x1+width, y1+height, 1f, 0f,
				x1+width, y1, 1f, 1f
				
		});
		floatBuffer.flip();
		
		byteBuffer = ByteBuffer.allocateDirect(12);
		byteBuffer.order(ByteOrder.nativeOrder());
		shortBuffer = byteBuffer.asShortBuffer();
		shortBuffer.clear();
		shortBuffer.put(new short[]{
				0,1,2,
				2,3,0
		});
		shortBuffer.flip();
		}else{
			
				byteBuffer = ByteBuffer.allocateDirect(32);
				byteBuffer.order(ByteOrder.nativeOrder());
				floatBuffer = byteBuffer.asFloatBuffer();
				floatBuffer.clear();
				floatBuffer.put(new float[]{
						x1, y1, 
						x1, y1+height,
						x1+width, y1+height,
						x1+width, y1
						
				});
				floatBuffer.flip();
				
				byteBuffer = ByteBuffer.allocateDirect(12);
				byteBuffer.order(ByteOrder.nativeOrder());
				shortBuffer = byteBuffer.asShortBuffer();
				shortBuffer.clear();
				shortBuffer.put(new short[]{
						0,1,2,
						2,3,0
				});
				shortBuffer.flip();
				floatBuffer.position(0);
			
		}
	}
	
	public Vertices(int width, int height, boolean isBitmapVertex){
		isTriangleVertex = false;
		if(isBitmapVertex){
			this.isBitmapVertex=true;
		
		byteBuffer = ByteBuffer.allocateDirect(64);
		byteBuffer.order(ByteOrder.nativeOrder());
		floatBuffer = byteBuffer.asFloatBuffer();
		floatBuffer.clear();
		floatBuffer.put(new float[]{
				-width/2, -height/2, 0f, 1f,
				-width/2, -height/2+height, 0f, 0f,
				-width/2+width, -height/2+height, 1f, 0f,
				-width/2+width, -height/2, 1f, 1f
				
		});
		floatBuffer.flip();
		
		byteBuffer = ByteBuffer.allocateDirect(12);
		byteBuffer.order(ByteOrder.nativeOrder());
		shortBuffer = byteBuffer.asShortBuffer();
		shortBuffer.clear();
		shortBuffer.put(new short[]{
				0,1,2,
				2,3,0
		});
		shortBuffer.flip();
		}else{
			
				byteBuffer = ByteBuffer.allocateDirect(32);
				byteBuffer.order(ByteOrder.nativeOrder());
				floatBuffer = byteBuffer.asFloatBuffer();
				floatBuffer.clear();
				floatBuffer.put(new float[]{
						-width/2, -height/2, 
						-width/2, -height/2+height,
						-width/2+width, -height/2+height,
						-width/2+width, -height/2
						
				});
				floatBuffer.flip();
				
				byteBuffer = ByteBuffer.allocateDirect(12);
				byteBuffer.order(ByteOrder.nativeOrder());
				shortBuffer = byteBuffer.asShortBuffer();
				shortBuffer.clear();
				shortBuffer.put(new short[]{
						0,1,2,
						2,3,0
				});
				shortBuffer.flip();
				floatBuffer.position(0);
			
		}
	}

	
	public FloatBuffer getVertices(){
		return floatBuffer;
	}
	
	public ShortBuffer getIndices(){
		return shortBuffer;
	}
	
	public boolean isBitmapVerex(){
		return isBitmapVertex;
	}
	
	public boolean isTriangleVerex(){
		return isTriangleVertex;
	}
}
